import { httpCreate, CommonResponse, getUrlPath } from './http';
import conf from './config';
const http = httpCreate('/srv/media');
const uploadHost = conf.uploadHost;

export const upload = {
	uploadSingle<T>(body: FormData) {
		return http.post<T, CommonResponse>('/single', body);
	},
	getUploadToken<T>(params: {
		uploadType: 'image' | 'video' | 'tape' | 'file';
		fileType?: string;
		fileName?: string;
	}) {
		return http.get<T, CommonResponse>('/fileStorage/uploadToken', { params });
	},
	async uploadImg<T>(data: File) {
		const tokenData = await this.getUploadToken({ uploadType: 'image' });
		const backend = encodeURIComponent(getUrlPath('/srv/media/fileStorage/one'));
		return await http.post<T, CommonResponse>(`file/image?backend=${backend}`, data, {
			headers: {
				token: tokenData.data.data.token,
			},
			baseURL: uploadHost + '/upload',
		});
	},
	async uploadVideo<T>(data: File) {
		const tokenData = await this.getUploadToken({ uploadType: 'video' });
		const backend = encodeURI(getUrlPath('/srv/media/fileStorage/one'));
		return await http.post<T, CommonResponse>(`/file/video?backend=${backend}`, data, {
			headers: {
				token: tokenData.data.data.token,
			},
			baseURL: uploadHost + '/upload',
		});
	},
};
